<template>
  <div class="borrower">
    <el-form :model="ruleForm"
             :rules="rules"
             ref="ruleForm"
             label-width="100px"
             class="yuyi-ruleForm">

      <el-form-item label="资料类型" prop="dataType">
        <el-select v-model="ruleForm.dataType" style="width: 100%" placeholder="请选择资料类型">
          <el-option v-for="(item, index) in zlList"
                     :key="index"
                     :label="item.dictLabel"
                     :value="item.dictLabel"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传" prop="paperAddress">
        <el-upload
          class="upload-btn"
          ref="upload"
          :action="action"
          :headers="headers"
          :on-success="handleAvatarSuccess"
          :limit="1"
          list-type="picture"
          :show-file-list="false">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        </el-upload>
      </el-form-item>
    </el-form>

    <div class="list-box" v-if="tableData.length>0">
      <el-table
        :data="tableData"
        border
        class="table">
        <el-table-column
          align="center"
          type="index"
          label="序号"
          width="60">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="dataType"
          label="资料类型">
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="paperAddress"
          label="存储路径">
        </el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button @click="handleDel(scope.row, scope.$index)"
                       type="text"
                       style="color: #F56C6C">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="fun-btn text-right" v-if="tableData.length>0">
      <el-button :loading="loading"
                 size="small"
                 type="primary"
                 @click="handleComplete">
        {{type === 'add' ? '完成' : '保存'}}
      </el-button>
      <el-button :loading="loading" size="small">重置</el-button>
    </div>

  </div>
</template>

<script>

import { getArchivesDicId } from '../../../../../api/dictionary/dictionary'
import { DEFAULT_ENTRYPOINT } from '../../../../../config/entrypoint'
import { getToken } from '@/util/auth'

export default {
  name: 'dataSubmit', // 资料提交
  props: {
    type: {
      type: String,
      default: 'add'
    },
    loading: {
      type: Boolean,
      default: false
    },
    formObj: {
      type: Object
    }
  },
  data () {
    return {
      zlList: [], // 资料类型
      ruleForm: {
        dataType: '',
        paperAddress: ''
      },
      autosize: {
        minRows: 4,
        maxRows: 8
      },
      rules: {
        dataType: [
          {
            required: true,
            message: '请输入活动名称',
            trigger: 'blur'
          }
        ],
        upload: [
          {
            required: true,
            message: '请选择上传文件',
            trigger: 'blur'
          }
        ]
      },
      subType: 'add',
      rowIndex: '',
      tableData: []
    }
  },
  mounted () {
    this.getDic()
  },
  computed: {
    headers () {
      const obj = {
        Authorization: 'bearer ' + getToken()
      }
      return obj
    },
    action () {
      const bucketType = 'use'
      return DEFAULT_ENTRYPOINT + `/yuyi-user-boot/api/sys/v1/sysUser/bucketType/${bucketType}/fileUpload`
    },
    obj () {
      const items = this.formObj
      return items
    }
  },
  watch: {
    formObj: {
      handler: function () {
        this.tableData = this.obj.zltjpab
      },
      deep: true
    }
  },
  methods: {
    getDic () {
      // 资料类型
      getArchivesDicId('datas_type').then(res => {
        this.zlList = res.data.data
        this.ruleForm.dataType = this.zlList[0].dictLabel
      })
    },
    handleAvatarSuccess (res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      // 登记/修改
      this.ruleForm.paperAddress = res.data

      if (this.type === 'edit') {
        // 条目修改时 新增
        const dataObj = JSON.parse(JSON.stringify(this.ruleForm))
        dataObj.idContinue = this.idContinue
        const addObj = {
          zltjpab: []
        }
        addObj.zltjpab.push(dataObj)
        this.$emit('handleAdd', addObj)
        return
      }

      this.tableData.push(JSON.parse(JSON.stringify(this.ruleForm)))
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    handleDel (row, index) {
      // 删除
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.type === 'add') {
          this.tableData.splice(index, 1)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }
        if (this.type === 'edit') {
          this.$emit('handleDel', row)
        }
      }).catch(() => {})
    },
    handleComplete () {
      // 完成
      this.obj.zltjpab = this.tableData
      this.$emit('handleComplete', this.obj)
    }
  }
}
</script>

<style lang="scss" scoped>

  .borrower {
    .yuyi-ruleForm {
      position: relative;
      padding-top: 40px;

      .yuyi-upload {
        position: absolute;
        right: 0px;
        z-index: 2;
        border: 1px dashed #ccc;

        .el-upload {
          border: 1px dashed #d9d9d9;
          border-radius: 6px;
          cursor: pointer;
          position: relative;
          overflow: hidden;

          &:hover {
            border-color: #409EFF;
          }
        }

        .avatar-uploader-icon {
          font-size: 28px;
          color: #8c939d;
          width: 160px;
          height: 160px;
          line-height: 160px;
          text-align: center;
        }

        .avatar {
          width: 178px;
          height: 178px;
          display: block;
        }
      }
    }

    .list-box {
      margin-top: 15px;
      margin-bottom: 15px;
    }
  }

</style>
